/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controller;

import common.Constant;
import dao.OrderDAO;
import dao.OrderDetailDAO;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.OrderDetailsModel;
import model.OrderModel;

/**
 *
 * @author ThanhToan
 */
public class OrderDetailController extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */

    private Integer mapParameterToModel(HttpServletRequest req
			,HttpServletResponse res
				,OrderModel orderModel) {
        int countRowSelect = 0;
        int count = Integer.parseInt(req.getParameter("txtNo").trim().toString());
        if (req.getParameter("totalAmount").toString().trim() != null) {
			orderModel.setTotalAmount(req.getParameter("totalAmount").toString().trim()); }
        if (req.getParameter("discount").toString().trim() != null) {
			orderModel.setDiscount(req.getParameter("discount").toString().trim()); }
        orderModel.setAccID(req.getSession().getAttribute("userID").toString());
        ArrayList<OrderDetailsModel> orderDetailList = new ArrayList<OrderDetailsModel>();
        for(Integer i = 1; i<=count; i++){
            OrderDetailsModel orderDetailModel = new OrderDetailsModel();
            if (req.getParameter("txtProductID"+i.toString()) != null) {
			orderDetailModel.setProductID(req.getParameter("txtProductID" + i.toString().trim())); }
            if (req.getParameter("txtProductName"+i.toString()) != null) {
			orderDetailModel.setProductName(req.getParameter("txtProductName" + i.toString().trim())); }
            if (req.getParameter("txtPrice"+i.toString()) != null) {
			orderDetailModel.setPrice(req.getParameter("txtPrice"+i.toString().trim())); }
            if (req.getParameter("txtNum"+i.toString()) != null) {
			orderDetailModel.setNumber(req.getParameter("txtNum"+i.toString().trim())); }
            if (req.getParameter("txtAmount"+i.toString()) != null) {
			orderDetailModel.setValue(req.getParameter("txtAmount"+i.toString().trim())); }
            if (req.getParameter("chkRow" + i.toString()) != null
					&& req.getParameter("action").equals("remove")) {
			// Row nay dang bi chon de DELETE ==> ko dua vao Object Model
		String id = req.getParameter("txtID"+i.toString().trim());
                OrderDetailDAO.deleteOrderDetailsByID(id);
                countRowSelect++;
		req.setAttribute("msgDeleteRow", "remove");
            }
            else{
                orderDetailList.add(orderDetailModel);
            }
        }
        orderModel.setOrderDetailList(orderDetailList);
        return countRowSelect;
    }

    protected void actionRequest(HttpServletRequest request, HttpServletResponse response)
                                                        throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String URL = "";
        RequestDispatcher rd = request.getRequestDispatcher(URL);
//        HttpSession session = request.getSession(true);
        String status = request.getParameter("txtStatus").toString();
        try {
            String action = request.getParameter("action");
            OrderModel orderModel = new OrderModel();
            Integer countRowSelected = mapParameterToModel(request, response, orderModel);
            if(action.equals("remove")){
                Constant.NUM_ROWS -= countRowSelected;
                URL = "orderDetails.jsp?remove=yes&&status="+status;
            }
            if(action.equals("update")){
                orderModel.setId(request.getParameter("txtOrderID"));
                if(OrderDAO.updateOrder(orderModel)){
                    URL = "orderDetails.jsp?update=yes&&status="+status;
                }
            }
//            session.setAttribute(Constant.SHOPPING_CARD, orderModel.getOrderDetailList());
            request.setAttribute(Constant.SHOPPING_CARD, orderModel.getOrderDetailList());
        }catch(Exception e){
            e.printStackTrace();
        }
        rd = request.getRequestDispatcher(URL);
        rd.forward(request, response);
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        doPost(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        actionRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
